package com.googlecode.bip.xml.importe;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.NamingException;
import com.googlecode.bip.entity.IllegalValueException;

/**
 * Handle XML import to MySQL database
 */
// not public
class SQLQuestion extends Commiter implements Commitable
{
    private Integer idquestion;
    private SQLTopic parentSQLTopic;
    private Integer order;
    private Integer isactive;
    private Integer minvalue;
    private Integer maxvalue;
    private Integer defaultvalue;

    private List<SQLQuestionLocal> childSQLQuestionLocalList = new ArrayList<SQLQuestionLocal>();

    SQLQuestion( final SQLStatementCollection sqlCollection )
    {
        super( sqlCollection );
    }

    void setIdQuestion( final int idquestion )
    {
        if( idquestion == 0 ) {
            throw new IllegalArgumentException( "idquestion=0 not allowed" );
            }

        this.idquestion = idquestion;
    }

    int getIdQuestion()
    {
        return this.idquestion;
    }

    public void setParent( final SQLTopic parent )
    {
        this.parentSQLTopic = parent;
    }

    void setOrder( final int order )
    {
        this.order = order;
    }

    void setIsActive( final int isactive )
    {
        this.isactive = isactive;
    }

    void setIsactive( final int isactive )
    {
        this.isactive = isactive;
    }

    void setMinValue( final int minvalue )
    {
        this.minvalue = minvalue;
    }

    void setMaxValue( final int maxvalue )
    {
        this.maxvalue = maxvalue;
    }

    void setDefaultValue( final int defaultvalue )
    {
        this.defaultvalue = defaultvalue;
    }

    public void add( final SQLQuestionLocal child )
    {
        if( child == null ) {
            throw new NullPointerException("child SQLQuestion is null");
            }

        childSQLQuestionLocalList.add( child );
    }

    @Override
    public void commit()
        throws NamingException, SQLException, IllegalValueException
    {
        System.out.println( ">=> " + this.toString() );

        if( order == null ) {
            throw new IllegalValueException( "order is null" );
            }
        if( isactive == null ) {
            throw new IllegalValueException( "isactive is null" );
            }
        if( minvalue == null ) {
            throw new IllegalValueException( "minvalue is null" );
            }
        if( maxvalue == null ) {
            throw new IllegalValueException( "maxvalue is null" );
            }
        if( defaultvalue == null ) {
            throw new IllegalValueException( "defaultvalue is null" );
            }

        // Check min, max, default consitance.
        if( minvalue > maxvalue ) {
            throw new IllegalValueException( "min:" + minvalue + " > max:" + maxvalue );
            }
        if( defaultvalue < minvalue ) {
            throw new IllegalValueException( defaultvalue, minvalue, maxvalue );
            }
        if( defaultvalue > maxvalue ) {
            throw new IllegalValueException( defaultvalue, minvalue, maxvalue );
            }

        String parentIdTopicStr = SQLTopic.getIdTopicSQLString( this.parentSQLTopic );
        String sql = "INSERT INTO `questions` (`idquestion`, `idtopic`, `order`, `isactive`, `minvalue`, `maxvalue`, `defaultvalue`) VALUES ('"
            + idquestion
            + "',"
            + parentIdTopicStr
            + ",'"
            + order
            + "','"
            + isactive
            + "','"
            + minvalue
            + "','"
            + maxvalue
            + "','"
            + defaultvalue
            + "') ON DUPLICATE KEY UPDATE `idtopic`="
            + parentIdTopicStr
            + ", `order`='"
            + order
            + "', `isactive`='"
            + isactive
            + "', `minvalue`='"
            + minvalue
            + "', `maxvalue`='"
            + maxvalue
            + "', `defaultvalue`='"
            + defaultvalue
            + "';";

        super.addSQLStatement( sql );

        for( SQLQuestionLocal c : childSQLQuestionLocalList ) {
            c.setParent( this );
            c.commit();
            }
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
        return "SQLQuestion [idquestion=" + idquestion
                + ", order=" + order
                + ", isactive=" + isactive
                + ", minvalue=" + minvalue
                + ", maxvalue=" + maxvalue
                + ", defaultvalue=" + defaultvalue
                + "]";
    }

}
